package meijuli.com.join51.business.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.utils.StringUtil;

/**
 * 作者:Ryan
 * 描述:关于聚惠侠
 *
 * @author jack
 */
public class FragmentAboutApp extends BaseFragment {
    public static final String KEY_LOADING_URL = "loadUrl";
    public static final String KEY_TITLE = "title";
    public String loadUrl;
    public String title;
    WebView mWebView;
    TextView mToolbarTitle;
    TextView mTextMenu;
    Toolbar mToolbar;

    public static FragmentAboutApp newInstance(String loadUrl, String title) {
        Bundle args = new Bundle();
        args.putString(KEY_LOADING_URL, loadUrl);
        args.getString(KEY_LOADING_URL, title);
        FragmentAboutApp fragment = new FragmentAboutApp();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_about_app, container, false);
        mWebView = view.findViewById(R.id.webView);
        mToolbarTitle = view.findViewById(R.id.toolbar_title);
        mTextMenu = view.findViewById(R.id.text_menu);
        mToolbar = view.findViewById(R.id.toolbar);
        return view;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getArguments() == null) {
            return;
        }
        loadUrl = getArguments().getString(KEY_LOADING_URL);
        title = getArguments().getString(KEY_TITLE);

        mWebView.setVerticalScrollBarEnabled(true);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.requestFocusFromTouch();
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showLoadingDialog();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissDialog();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        if (StringUtil.isEmpty(loadUrl)) {
            mWebView.loadUrl(getActivity().getString(R.string.appWebSite));
        } else {
            mWebView.loadUrl(loadUrl);
        }

        initToolBar();
    }

    private void initToolBar() {
        mToolbar.setTitle("");
        mToolbarTitle.setText("聚惠侠平台商家入驻协议");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
    }


}
